package org.refresh.core.intelligence.emf.tests;

import java.util.List;

import junit.framework.TestCase;

import org.eclipse.emf.examples.extlibrary.Book;
import org.eclipse.emf.examples.extlibrary.EXTLibraryFactory;
import org.eclipse.emf.examples.extlibrary.EXTLibraryPackage;
import org.refresh.core.intelligence.emf.OCLConstraint;
import org.refresh.emf.EMFIntelligence;
import org.refresh.prolog.PrologConstraint;
import org.refresh.prolog.PrologEvaluator;

/******************************************************************************
 * Copyright (c) 2005, 2006 Jules White.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Jules White - initial API and implementation 
 ****************************************************************************/
public class PrologEvaluatorTest extends TestCase {
	
	public void testSimplePrologConstraint(){
		EMFIntelligence kb = new EMFIntelligence();
		PrologEvaluator eval2 = new PrologEvaluator(kb);
		kb.getConstraintEvaluators().put(PrologConstraint.TYPE, eval2);
		kb.getKnowledgeManagers().add(eval2);

		OCLConstraint con = new OCLConstraint("not(self.title = target.title)");
		OCLConstraint con1 = new OCLConstraint("target.title = target.title");
		PrologConstraint pcon = new PrologConstraint("self_title(Self,N),self_title(Target,N2),not(N1 == N2)");
		EXTLibraryFactory fact = EXTLibraryPackage.eINSTANCE.getEXTLibraryFactory();
		Book f1 = fact.createBook();
		Book f2 = fact.createBook();
		Book f3 = fact.createBook();
		f1.setTitle("foo");
		f2.setTitle("foo2");
		f3.setTitle("foo");
		
		kb.add(f1);
		kb.add(f2);
		kb.add(f3);
		
		kb.add(f1.eClass(), "different", f1.eClass(), con1);
		kb.add(f1.eClass(), "different", f1.eClass(), con);
		kb.add(f1.eClass(), "different", f1.eClass(), pcon);
		
//		System.out.println("########################");
//		System.out.println(eval2.getKB().snapshot());
//		System.out.println("########################");
		
		List valid = kb.validTargets(f1, "different");
		assertEquals(1, valid.size());
		assertEquals(f2, valid.get(0));
	}
}
